<template>
  <div class="app-container">
    <!-- 功能区 -->
    <transition name="formFade">
      <el-card class="operate-container" shadow="never" v-if="showSearch">
        <el-form :inline="true" :model="listQuery" @submit.native.prevent>
          <el-form-item label="资源名称" class="form-item">
            <el-input v-model="listQuery.assetName" clearable class="filter-item" placeholder="资源名称"/>
          </el-form-item>
          <el-form-item label="开始日期:" class="form-item">
            <el-date-picker
              class="filter-item"
              v-model="listQuery.startDate"
              align="right"
              type="date"
              placeholder="选择开始日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束日期:" class="form-item">
            <el-date-picker
              class="filter-item"
              v-model="listQuery.endDate"
              align="right"
              type="date"
              placeholder="选择结束日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="状态:" class="form-item">
            <el-select placeholder="请选择" style="width:100%;" clearable v-model="listQuery.result"
                       class="form-select" :popper-append-to-body="false" popper-class="select-popper">
              <el-option :value="0" label="空闲"></el-option>
              <el-option :value="1" label="出租"></el-option>
              <el-option :value="2" label="停用"></el-option>
              <el-option :value="3" label="注销"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="form-item">
            <el-button type="primary" size="mini" icon="el-icon-search" @click="getList">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </transition>

    <div class="table_content">
      <headerSearch addTitle="登记资产"
                    @handleAdd="handleAdd"
                    @creRefresh="getList"
                    @isSearch="showSearch = !showSearch"></headerSearch>
      <!--表格渲染-->
      <el-table ref="table" v-loading="loading" :data="list" style="width: 100%;" border
                :header-cell-style="{background: '#F8F8F9',padding: '6px 0 !important',color: '#515a6e'}">
        <af-table-column prop="assetName" fixed="left" label="资源名称" />
        <af-table-column prop="address" label="资产地址" />
        <el-table-column prop="biddingStatus" label="招投标状态" min-width="120">
          <template slot-scope="scope">
            {{{'0':'未招标','1':'开始招标','2':'招标完成','3':'合同签定'}[scope.row.biddingStatus]}}
          </template>
        </el-table-column>
        <af-table-column prop="addUserName" label="录入用户名" />
        <el-table-column prop="leaseType" label="出租方式" min-width="120">
          <template slot-scope="scope">
            {{{'0':'招投标出租','1':'非招投标出租'}[scope.row.leaseType]}}
          </template>
        </el-table-column>
        <af-table-column prop="measure" label="面积(平方米)" />
        <af-table-column prop="orgName" label="所属使用权单位" />
        <af-table-column prop="property" label="产权性质" />
        <el-table-column prop="status" label="状态" >
          <template slot-scope="scope">
            {{{'0':'空闲','1':'出租','2':'停用','3':'注销'}[scope.row.status]}}
          </template>
        </el-table-column>
        <af-table-column prop="content" label="其它信息" />
        <af-table-column prop="remark" label="备注" />
        <af-table-column prop="addTime" label="资产录入时间" />
        <el-table-column fixed="right" label="操作" min-width="80">
          <template slot-scope="scope">
            <el-button class="btn" type="text" size="small"
                       @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination v-show="total>0" :total="total"
                  :page.sync="listQuery.pageNum"
                  :limit.sync="listQuery.pageSize"
                  @pagination="getList" />

      <!--表单添加-->
      <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
        <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="130px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="资产/资源名称:" prop="projectName" class="form-item">
                <el-input v-model="editForm.projectName" class="filter-item" placeholder="资产/资源名称"/>
              </el-form-item>
              <el-form-item label="面积(平方米)" prop="measure" class="form-item">
                <el-input v-model="editForm.measure" class="filter-item" placeholder="面积"/>
              </el-form-item>
              <el-form-item label="所属使用权单位:" prop="orgName" class="form-item">
                <el-select v-model="editForm.orgName" placeholder="请选择" ref="selectReport"
                           popper-class="select-popper" class="form-select"
                           :popper-append-to-body="false">
                  <el-option
                    :value="editForm.orgID"
                    :label="editForm.orgName"
                    style="
                width: 450px;
                height: 200px;
                overflow: auto;
                background-color: #fff;
              "
                  >
                    <el-tree
                      :data="orgList"
                      :props="{value: 'orgID',label: 'orgName',}"
                      @node-click="handleNodeClick"
                    ></el-tree>
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="出租方式:" prop="leaseType" class="form-item">
                <el-select placeholder="请选择" style="width:100%;" clearable v-model="editForm.leaseType"
                           class="form-select" :popper-append-to-body="false" popper-class="select-popper">
                  <el-option :value="0" label="招投标出租"></el-option>
                  <el-option :value="1" label="非招投标出租"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="资产地址:" prop="address" class="form-item">
                <el-input v-model="editForm.address" class="filter-item" placeholder="产权性质"
                          type="textarea" :autosize="{ minRows: 1, maxRows: 2}"/>
              </el-form-item>
              <el-form-item label="产权性质:" prop="property	" class="form-item">
                <el-input v-model="editForm.property" class="filter-item" placeholder="资产地址"/>
              </el-form-item>
              <el-form-item label="其它信息:" prop="content	" class="form-item">
                <el-input v-model="editForm.content" class="filter-item" placeholder="其它信息"
                          type="textarea" :autosize="{ minRows: 1, maxRows: 4}"/>
              </el-form-item>
              <el-form-item label="备注:" prop="remark	" class="form-item">
                <el-input v-model="editForm.remark" class="filter-item" placeholder="备注"
                          type="textarea" :autosize="{ minRows: 1, maxRows: 4}"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click.native="dialogFormVisible=false">取消</el-button>
          <el-button type="primary" size="mini" @click="createData">
            {{dialogStatus == 'create'?'添加':'保存'}}
          </el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { doPost, doGet, doFile, orglistChild } from '@/api/index'
  import Pagination from '@/components/Pagination/index'
  import headerSearch from '@/components/headerSearch/headerSearch'

  export default {
    name: "index",
    components: {Pagination, headerSearch},
    data() {
      return {
        loading: false,
        list: [
          {assetName: '测试资源', biddingStatus: 0,measure: 150,leaseType: 0,status: 1}
        ],
        total: 0,
        listQuery: {
          pageNum: 1,
          pageSize: 20,
          assetName: '',
          endDate: '',
          result: '',
          startDate: ''
        },
        showSearch: true,
        dialogStatus: '',
        textMap:{
          update: '编辑',
          create: '新增'
        },
        dialogFormVisible: false,
        editForm: {
          assetName: '',
          address: '',
          content: '',
          leaseType: '',
          measure: '',
          orgName: '',
          property: '',
          remark: '',
        },
        editFormRules: {
        },
        orgList: []
      }
    },
    created() {
      // this.getList()
      this.orgGetList()
    },
    methods: {
      orgGetList(){
        orglistChild().then(res=> {
          this.orgList = res.data
        }).catch(error => {
          this.orgList = []
        })
      },
      getList() {
        this.loading = true
        doPost('asset/page', this.listQuery).then(res => {
          this.loading = false
          this.list = res.data.list
          this.total = res.data.total
        }).catch(error => {
          this.list = []
          this.total = 0
          this.loading = false
        })
      },
      handleAdd() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.editForm = {
          assetName: '',
          address: '',
          content: '',
          leaseType: '',
          measure: '',
          orgName: '',
          property: '',
          remark: '',
        }
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      handleEdit(index, row) {
        this.$router.push({ name: 'resourceDetail', params: { id: row.id }})
      },
      //提交表单
      createData() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doPost(`asset/${this.dialogStatus == 'create' ? 'add' : 'update'}`,this.editForm).then(res => {
              if (res.code == 200){
                this.dialogFormVisible = false
                this.getList()
                this.$message.success({
                  message: this.dialogStatus == 'create'?'提交成功':'保存成功',
                  type: 'success'
                })
              } else {
                this.$message.error(res.message)
              }
            }).catch(error => {
              this.$message.error(error.message)
            })
          }
        })
      },
      //选择部门
      handleNodeClick(val) {
        this.$set(this.editForm, "orgID", val.orgID);
        this.$set(this.editForm, "orgName", val.orgName);
        if (!val.children || val.children.length == 0) {
          this.$refs.selectReport.blur();
        }
      },
    }
  }
</script>

<style scoped>
</style>
